﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Xml;
using System.Text;
using System.Windows.Forms;
using EZBarTender_Wrapper.SqlExecutor;


namespace EZBarTender_Wrapper
{
	[RunInstaller(true)]
	public partial class Installer1 : System.Configuration.Install.Installer
	{
		public Installer1()
		{
			InitializeComponent();
		}

		public override void Uninstall(IDictionary savedState)
		{
			base.Uninstall(savedState);
			string targetDir = Context.Parameters["targetdir"];
			try
			{
				if (Directory.Exists(targetDir)) Directory.Delete(targetDir, true);
			}
			catch
			{
				return;
			}
		}

		public override void Install(IDictionary stateSaver)
		{
			base.Install(stateSaver);
			string targetDir = Context.Parameters["targetdir"];
			string localConnection = "Data Source=" + Context.Parameters["localserver"] + ";";
			localConnection += "User ID=" + Context.Parameters["localUserId"] + ";";
			localConnection += "Password=" + Context.Parameters["localPassword"] + ";";
			localConnection += "Initial Catalog=" + Context.Parameters["localDatabase"] + ";";

			string corpConnection = "Data Source=" + Context.Parameters["corpServer"] + ";";
			corpConnection += "User ID=" + Context.Parameters["corpUserId"] + ";";
			corpConnection += "Password=" + Context.Parameters["corpPassword"] + ";";
			corpConnection += "Initial Catalog=" + Context.Parameters["corpDatabase"] + ";";

			string configFilePath = Path.Combine(targetDir, "secondconfig.config");
			IDictionary<string, string> userConfiguration = new Dictionary<string, string>();

			userConfiguration["LocalConnectionString"] = localConnection;
			userConfiguration["CorpConnectionString"] = corpConnection;
			userConfiguration["RegularFileLocation"] = Path.Combine(targetDir, "labels\\newReg.btw");
			userConfiguration["SpecialFileLocation"] = Path.Combine(targetDir, "labels\\newSpecial.btw");
			userConfiguration["SmartBuyFileLocation"] = Path.Combine(targetDir, "labels\\newSmartBuy.btw");
			userConfiguration["RegularSmallFileLocation"] = Path.Combine(targetDir, "labels\\smallshelflabel.btw");
			userConfiguration["SpecialSmallFileLocation"] = Path.Combine(targetDir, "labels\\smallspeciallabel.btw");
			userConfiguration["SmartBuySmallFileLocation"] = Path.Combine(targetDir, "labels\\smartbuysmalllabel.btw");
			userConfiguration["DiscontinuedSmallFileLocation"] = Path.Combine(targetDir, "labels\\discontinuedsmallshelflabel.btw");
			userConfiguration["DiscontinuedRegularFileLocation"] = Path.Combine(targetDir, "labels\\newRegularDisco.btw");
			userConfiguration["DiscontinuedSpecialFileLocation"] = Path.Combine(targetDir, "labels\\newSpecialDisco.btw");
			userConfiguration["XLarge1UpFileLocation"] = Path.Combine(targetDir, "labels\\l1upsign.btw");
			userConfiguration["Large1UpFileLocation"] = Path.Combine(targetDir, "labels\\1upsign.btw");
			userConfiguration["Medium1UpFileLocation"] = Path.Combine(targetDir, "labels\\m1upsign.btw");
			userConfiguration["Small1UpFileLocation"] = Path.Combine(targetDir, "labels\\s1upsign.btw");
			userConfiguration["2UpFileLocation"] = Path.Combine(targetDir, "labels\\2upSign.btw");
			userConfiguration["4UpFileLocation"] = Path.Combine(targetDir, "labels\\4upSign.btw");
			userConfiguration["SBXLarge1UpFileLocation"] = Path.Combine(targetDir, "labels\\l1upsignSB.btw");
			userConfiguration["SBLarge1UpFileLocation"] = Path.Combine(targetDir, "labels\\1upsignSB.btw");
			userConfiguration["SBMedium1UpFileLocation"] = Path.Combine(targetDir, "labels\\m1upsignSB.btw");
			userConfiguration["SBSmall1UpFileLocation"] = Path.Combine(targetDir, "labels\\s1upsignSB.btw");
			userConfiguration["SB2UpFileLocation"] = Path.Combine(targetDir, "labels\\2upSignSB.btw");
			userConfiguration["SB4UpFileLocation"] = Path.Combine(targetDir, "labels\\4upSignSB.btw");
			userConfiguration["CatalinaFileLocation"] = Path.Combine(targetDir, "labels\\small catalina.btw");
            userConfiguration["DiscoSmallLocation"] = Path.Combine(targetDir, "labels\\discosmallspeciallabel.btw");
			userConfiguration["XmlFileLocation"] = Path.Combine(targetDir, "print.xml");

			ConfigGenerator.WriteExternalAppConfig(configFilePath, userConfiguration);
			
			MSSqlExecutor sqlExecutor = new MSSqlExecutor(localConnection);
			sqlExecutor.GenerateStatements(Path.Combine(targetDir , "Resources\\initialize.sql"));
			sqlExecutor.Execute();

		}

		public class ConfigGenerator
		{
			public static void WriteExternalAppConfig(String configFilePath, IDictionary<String, String> userConfiguration)
			{
				using (XmlTextWriter xw = new XmlTextWriter(configFilePath, Encoding.UTF8))
				{ 
					xw.Formatting = Formatting.Indented;
					xw.Indentation = 4;
					xw.WriteStartElement("appSettings");

					foreach (KeyValuePair<String, String> pair in userConfiguration)
					{
						xw.WriteStartElement("add");
						xw.WriteAttributeString("key", pair.Key);
						xw.WriteAttributeString("value", pair.Value);
						xw.WriteEndElement();
					}
					xw.WriteEndElement();
					xw.Close();
				}
			}
		}
	}
}
